﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using DAL;
using BLL;

public partial class admin_Categories_Details : System.Web.UI.Page
{
  CategoryBLL data = new CategoryBLL();
  private static int maxLevel;
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        Session["url_back_admin"] = Request.RawUrl;

        maxLevel = GetLevelConfig();
        LoadTitle();
        LoadCategory();
        if (Request.QueryString["Id"] != null)
          Filldata();
      }
      if (Request.QueryString["Id"] != null)
          dropParent.Enabled = false;
    }
    private void Filldata()
    {
      int id = Convert.ToInt32(Request.QueryString["Id"]);
      Category category = data.GetById(id);
      txtName.Text = category.Name;
      fckEditor.Value = HttpUtility.HtmlDecode(category.Description);
      fckEditor1.Value = HttpUtility.HtmlDecode(category.Content);
      dropParent.SelectedValue = category.Parent.ToString();
      if (category.isEnable) radioEnable.Checked = true;
      else radioVisible.Checked = true;
    }
    private void ShowChild(int parentId, string space)
    {
      List<Category> listDataSource = data.GetByParent(parentId);

      foreach (Category item in listDataSource)
      {
        ListItem obj = new ListItem(space + item.Name, item.Id.ToString());
        dropParent.Items.Add(obj);

        ShowChild(item.Id, space + "- - - ");
      }
    }
    private void LoadCategory()
    {
      dropParent.Items.Clear();
      ShowChild(0, " - - ");
      ListItem itemRoot = new ListItem("Danh mục gốc", "0");
      dropParent.Items.Insert(0, itemRoot);
    }
    private void LoadTitle()
    {
      if (Request.QueryString["Id"] != null)
      {
        h3Title.InnerText = "Chỉnh sửa danh mục tin tức";        
      }
    }
    private bool isValidate()
    {
      if (txtName.Text == "")
      {
        Myfunction.ThongBao(lbThongBao, "Tên danh mục không được để trống !");
        return false;
      }
      return true;
    }
    private void ResetForm()
    {
      txtName.Text = "";
      radioEnable.Checked = true;
    }   
    private int GetLevelConfig()
    {
      XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.XML");
      var xLevel = xElement.Descendants("MaxLevel").Descendants("CategoryNews");
      XElement level = xLevel.First();
      return Convert.ToInt32(level.Value);
    }
    private void Update()
    {
      int idParent = Convert.ToInt32(dropParent.SelectedValue);
      int id = Convert.ToInt32(Request.QueryString["Id"]);
      Category category = new Category();
      Category categoryCurrent = data.GetById(id);

      category.Id = id;
      category.Name = txtName.Text;
      category.Description = HttpUtility.HtmlEncode(fckEditor.Value);
      category.Content = HttpUtility.HtmlEncode(fckEditor1.Value);
      category.Sort = data.GetMaxSort(idParent);
      category.CreateDate = categoryCurrent.CreateDate;
      category.isEnable = (radioEnable.Checked ? true : false);
      category.Parent = idParent;
      category.isDelete = categoryCurrent.isDelete;     
 
      if (idParent == 0) category.Level = 1;
      else
      {
        category.Level = (byte)(data.GetById(idParent).Level + 1);
        if (category.Level > maxLevel)
        {
          Myfunction.ThongBao(lbThongBao,"Danh mục chỉ được tối đa " + maxLevel + " cấp");
          return;
        }
      }

      category.Image = categoryCurrent.Image;
      if (radioDeAnh.Checked)
      {
        if (fileUpload.HasFile)
        {
            if (category.Image == null || category.Image == "")
                category.Image = Myfunction.CopyFile(fileUpload.PostedFile, "Images/Category", category.Name);
            else
                category.Image = Myfunction.UpdateFile(fileUpload.PostedFile, "Images/Category", category.Image, category.Name);
        }
      }
     
      data.Update(category);

      Myfunction.ThongBao(lbThongBao,"Danh mục đã được cập nhật");
    }
    private void Add()
    {
      int idParent = Convert.ToInt32(dropParent.SelectedValue);
      Category category = new Category();

      category.Name = txtName.Text;
      category.Description = HttpUtility.HtmlEncode(fckEditor.Value);
      category.Content = HttpUtility.HtmlEncode(fckEditor1.Value);
      category.Sort = data.GetMaxSort(idParent);
      category.CreateDate = DateTime.Now;
      category.isEnable = (radioEnable.Checked ? true : false);
      category.Parent = idParent;
      category.isDelete = true;

      if (idParent == 0) category.Level = 1;
      else
      {
        category.Level = (byte)(data.GetById(idParent).Level + 1);
        if (category.Level > maxLevel)
        {
          Myfunction.ThongBao(lbThongBao, "Danh mục chỉ được tối đa " + maxLevel + " cấp");
          return;
        }
      }
      if (radioDeAnh.Checked)
      {
        if (fileUpload.HasFile)
        {
            category.Image = Myfunction.CopyFile(fileUpload.PostedFile, "Images/Category", category.Name);
        }
      }
      data.Add(category);
      Myfunction.ThongBao(lbThongBao, "Danh mục đã được cập nhật");
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
      if ((int)Session["Role"] != 1)
      {
        Myfunction.ThongBao(lbThongBao, "Bạn không có quyền thực hiện thao tác này ! ");
        return;
      }
      try
      {
        lbThongBao.Text = "";
        if (isValidate())
        {
          if (Request.QueryString["Id"] == null)
          {
            Add();
            ResetForm();
            LoadCategory();
          }
          else
          {
            Update();
          }
        }
      }
      catch (Exception ex)
      {
        Myfunction.ThongBao(lbThongBao, ex.Message);
      }
    }
    protected void radioBoAnh_CheckedChanged(object sender, EventArgs e)
    {
      if (radioBoAnh.Checked)
      {
        trFileUpload.Visible = false;
      }
      else
        trFileUpload.Visible = true;
    }
}